﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class views_ucproducts : System.Web.UI.UserControl
{
    DataProvider datapro = new DataProvider();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            int page = 1;
            string cateid = "%";
            if (Request.QueryString["page"] != null)
                page = int.Parse(Request.QueryString["page"].ToString());
            if (Request.QueryString["cateid"] != null)
                cateid = Request.QueryString["cateid"].ToString();

            load_cache_products_view();
            load_product(page, 20, cateid);
            load_title();
        }
    }
    public void load_title()
    {
        DataTable dt_leftmenu_root = (DataTable)Cache["leftmenu_root"];
        DataTable dt_leftmenu = (DataTable)Cache["leftmenu"];

        if (Request.QueryString["cateid"] != null)
        {
            string cateid = Request.QueryString["cateid"].ToString();
            DataRow[] dr_c = dt_leftmenu.Select("categoryid = " + cateid);
            DataRow[] dr_r = dt_leftmenu_root.Select("categoryid = " + dr_c[0].ItemArray[2].ToString());
            lbtitle.Text = dr_r[0].ItemArray[1].ToString() + " / " + dr_c[0].ItemArray[1].ToString();

            Page.Title = "Shop Flower :: " + lbtitle.Text;
        }
    }
    public void load_product(int pagenumber, int itemview, string cateid)
    {
        string strcateid="";
        if (cateid == "%")
            strcateid = "";
        else
            strcateid = "categoryid = " + cateid;
        DataTable dt_product_new = (DataTable)Cache.Get("products_view");
        DataRow[] dr_product = dt_product_new.Select(strcateid);
        string strproduc = "<ul class=\"products\" style=\"margin: 0pt; padding: 0pt; position: relative; \">";
        string strli = @"<li class='first' style=' float: left; width: 150px; height: 227px;'>
                            <a href='{0}' class='product' title='#' alt='#' rel='product-results'>
                                <span class='figure'>
                                    <img width='150' src='{1}'>
                                    {2}
                                </span>
                                <h2 style='height:16px;'>
                                    {3}
                                </h2>
                                <div class='price-details'>
                                    <span class='price-new'>
                                        {4}đ</span>
                                    <span style='float: none; display: block' class='price-old'>
                                        {5}</span>
                                </div>
                                <div class='view-sells'>
                                    <span class='views'>
                                        View : {6}</span>
                                    <span style='float: none; display: block' class='sells'>
                                        Sell : {7}</span>
                                </div>
                            </a>
                        </li>";
        float discount;
        string strdiscount = "";
        string strprice = "";
        string stroldprice = "";
        int itemstar = (pagenumber - 1) * itemview;
        int itemend = (pagenumber * itemview) - 1;
        for (int i = itemstar; i < dr_product.Length && i <= itemend; i++)
        {
            strprice = dr_product[i].ItemArray[10].ToString();
            strdiscount = "";
            stroldprice = "";
            if (dr_product[i].ItemArray[12].ToString().Trim() != "")
            {
                discount = float.Parse(dr_product[i].ItemArray[12].ToString().Trim());
                strdiscount = string.Format("<span class=\"promotion_tag\" >{0}%</span>", discount);
                stroldprice = dr_product[i].ItemArray[10].ToString().Trim();
                strprice = ((int.Parse(dr_product[i].ItemArray[10].ToString().Trim()) * discount) / 100).ToString();
            }
            strproduc = strproduc + string.Format(strli,
               "index.aspx?proid=" + dr_product[i].ItemArray[0].ToString(),
               "images/products/" + dr_product[i].ItemArray[6].ToString(),
               strdiscount,
               dr_product[i].ItemArray[1].ToString(),
               strprice,
               stroldprice,
               dr_product[i].ItemArray[7].ToString(),
               dr_product[i].ItemArray[8].ToString());
        }
        lbproductview.Text = strproduc + "</ul>";
        load_page_number(pagenumber, itemview, dr_product);
    }
    public void load_page_number(int pagenumber, int itemview, DataRow[] dr)
    {
        string strpage = @"<div class='sorter'>
                                <div class='ch-pagination' id='ch-pagination'>
                                    <span>
                                        <span class='totalRecord'>Tìm thấy <strong>{0}</strong> sản phẩm&nbsp;</span>
                                        {1}
                                            <span class='current'>{2}</span>&nbsp;
                                            {3}
                                        {4}
                                    </span>
                                </div>
                            </div>";

        string strpagenumber = "<a href='index.aspx?page={0}' class='command'>{0}</a>&nbsp;";
        //string strmore = "<a href='index.aspx?page={0}'  class='command'>...</a>&nbsp;";
        string strprevious = @"<a class='previous-off' {0}>&nbsp;«&nbsp;</a>&nbsp;
                               <a class='previous-off' {1}>&nbsp;‹&nbsp;</a>&nbsp;";
        string strnext = @"<a {0} class='next'>&nbsp;›&nbsp;</a>&nbsp;
                           <a {1} class='next'>&nbsp;»&nbsp;</a>&nbsp;";

        string strpagenumbers = "";
        int total_page = dr.Length / itemview;

        if (Request.QueryString["cateid"] != null)
        {
            string cateid = Request.QueryString["cateid"].ToString();
            strpagenumber = "<a href='index.aspx?cateid=" + cateid + "&page={0}' class='command'>{0}</a>&nbsp;";
        }
        if (dr.Length % itemview != 0)
            total_page++;
        else if (total_page == 0)
            total_page = 1;

        for (int i = pagenumber; i < total_page && i < pagenumber + 5; i++)
        {

            strpagenumbers = strpagenumbers + string.Format(strpagenumber, i + 1, i + 1);
        }

        if (pagenumber == 1)
        {
            strprevious = string.Format(strprevious, "disabled='disabled'", "disabled='disabled'");

        }
        else
        {
            if (Request.QueryString["cateid"] != null)
            {
                string cateid = Request.QueryString["cateid"].ToString();
                strprevious = string.Format(strprevious,
                       "href='index.aspx?cateid=" + cateid + "&page=1'",
                       "href='index.aspx?cateid=" + cateid + "&page=" + (pagenumber - 1).ToString() + "'");
            }
            else
            {
                strprevious = string.Format(strprevious,
                    "href='index.aspx?page=1'",
                    "href='index.aspx?page=" + (pagenumber - 1).ToString() + "'");
            }
        }

        if (pagenumber == total_page)
        {
            strnext = string.Format(strnext, "disabled='disabled'", "disabled='disabled'");
        }
        else
        {

            if (Request.QueryString["cateid"] != null)
            {
                string cateid = Request.QueryString["cateid"].ToString();
                strnext = string.Format(strnext,
                    "href='index.aspx?cateid=" + cateid + "&page=" + (pagenumber + 1).ToString() + "'",
                    "href='index.aspx?cateid=" + cateid + "&page=" + total_page + "'");
            }
            else
            {
                strnext = string.Format(strnext,
                        "href='index.aspx?page=" + (pagenumber + 1).ToString() + "'",
                        "href='index.aspx?page=" + total_page + "'");
            }
        }

        //if (pagenumber - 5 > 1)
        //{
        //    strpagenumbers = string.Format(strmore, pagenumber - 1) + strpagenumbers;
        //}
        //else if (pagenumber + 5 < total_page + 1)
        //{
        //    strpagenumbers = strpagenumbers + string.Format(strmore, pagenumber + 6);
        //}
        lbpagenumber.Text = string.Format(strpage, 
            dr.Length.ToString(), 
            strprevious,
            pagenumber,
            strpagenumbers,
            strnext);
    }
    public void load_cache_products_view()
    {
        DataTable dt_leftmenu = datapro.sp_select("sp_view_productview");
        Cache.Remove("products_view");
        Cache.Insert("products_view", dt_leftmenu);
    }
}